package org.sang.controller.leave;

import org.sang.bean.*;
import org.sang.common.UserUtils;
import org.sang.service.DailyReportService;
import org.sang.service.LeaveWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/leave")
public class LeaveWorkController {

	@Autowired
    LeaveWorkService leaveWorkService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Map<String, Object> getLeaveWorkByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, @RequestParam(defaultValue = "") String keywords,String beginDateScope) {
        Map<String, Object> map = new HashMap<>();
        User user=UserUtils.getCurrentUser();
        List<Role> roles=user.getRoles();
        Long userId=user.getId();
        boolean  isAdmin=false;
        for(Role role:roles){
            if(role.isAdmin()){
                userId=null;
                isAdmin=true;
            }
        }
        List<LeaveWork> leaveWorkByPage = leaveWorkService.getLeaveWorkByPage(page, size, keywords, userId,beginDateScope);
        Long count = leaveWorkService.getCountByKeywords(keywords,userId, beginDateScope);
        map.put("leavelist", leaveWorkByPage);
        map.put("count", count);
        map.put("isAdmin", isAdmin);
        return map;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RespBean addLeaveWork(LeaveWork leaveWork) {
        if (leaveWorkService.addLeaveWork(leaveWork) == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }
    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    public RespBean delLeaveWork(@PathVariable String ids) {
        if (leaveWorkService.delLeaveWorkById(ids)) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }
}
